import smtplib
from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

# 封装邮箱发送
from email.header import Header
# 荣联云
from ronglian_sms_sdk import SmsSDK
import json

class Phone_Code:
    def __init__(self):
        # 获取短信验证
        self.accId = '8a216da87a332d53017ae5cceb6933d0'  # 自己的id
        self.accToken = 'd5b44fc241e24f1683cd325dada9ecad'  # 自己的ken的码
        self.appId = '8a216da87a332d53017ae5ccec4e33d6'

    def send_message(self,mobile, code, time):
        sdk = SmsSDK(self.accId, self.accToken, self.appId)
        tid = '1'
        datas = (code, time)
        # mobile手机号,code验证码,datas时间内容
        resp = sdk.sendMessage(tid, mobile, datas)
        result = json.loads(resp)
        if result['statusCode'] == '000000':
            return 1
        return 2

    # 邮箱封装
    def send_mail(self,to, title, content):
        # def send_mail(to,title,content):
        # 发送者
        _user = "2409248564@qq.com"  # 发送人
        # smtp秘钥
        # _pwd="您刚申请的秘钥"
        _pwd = "golzzkfanvveeafi"  # 您申请的秘钥
        # 发送设置
        # content内容
        message = MIMEText(_text=content, _subtype="plain", _charset="utf-8")
        # 发送者_user  接受者to
        message['From'] = Header(_user, 'utf-8')
        message['To'] = Header(to, 'utf-8')
        # title标题

        message['Subject'] = Header(title, 'utf-8')
        # 添加内容
        # message.attach(content)
        # subject = 'Python SMTP 邮件测试'
        try:
            smtpObj = smtplib.SMTP_SSL(host='smtp.qq.com')
            smtpObj.connect('smtp.qq.com', 465)  # 465 为 SMTP 端口号
            smtpObj.login(_user, _pwd)
            smtpObj.sendmail(_user, to, message.as_string())
            print("邮件发送成功")
        except Exception as e:
            print("Error: 无法发送邮件")
            return 1
        return 2
